//A convenient wrapper for StringBuilder, keeps indent status
//>>>set custom escape character for args
//>>>set custom indent string
//>>>cout printer, memory printer

public class Printer {
   private final StringBuilder sb=new StringBuilder();
   private int indent=0;
   private final String INDENT_STRING="   ";
   
   private void pIndent() {
      for(int i=0; i<indent; i++) { sb.append(INDENT_STRING); }
   }
   public void indent() { indent++; }
   public void unindent() { indent--; }
   
   private boolean mayNeedIndent=false;
   public Printer ln() {
      sb.append("\n");
      mayNeedIndent=true;
      return this;
   }
   
   public Printer p(String s, Object ... args) {
      if(mayNeedIndent) {
         pIndent();
         mayNeedIndent=false;
      }
      int index=0;
      for(int i=0; i<s.length(); i++) {
         if(s.charAt(i)=='?') {
            sb.append(args[index++]);
         } else if(s.charAt(i)=='\n') {
            ln();
         } else {
            sb.append(s.charAt(i));
         }
      }
      return this;
   }
   //public Printer p(String s) { return p(s); }
   public Printer pn(String s) { p(s).ln(); return this; }
   public Printer pn(String s, Object ... args) { return p(s, args).ln(); }
   
   public String toString() { return sb.toString(); }
}
